package com.cheer.vo;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.cheer.entity.Applicant;
import com.cheer.entity.Organization;
import com.cheer.enums.OrgTypeEnum;
import com.cheer.enums.TitleEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

@Data
@Accessors(chain = true)
public class RegisterVO {

    private Integer registerStatus;

    private Integer title;

    private String titleYear;

    private Boolean awarded;

    private String contactPerson;

    private String contactPersonPhone;

    private String contactPersonPost;

    private String contactPersonBirth;

    private Integer personNumber;

    private Integer youthNumber;

    private String phone;

    private String email;

    private String address;

    private String remark;

    private Long orgId;

    private Long[] orgPath;

    private Boolean reviewTypeEnabled;

    private Integer reviewType;

    private Long joinReviewOrgId;

    private String usedName;

    private Long[] joinReviewOrgPath;

    public static RegisterVO build(Applicant applicant, Organization organization, Organization joinOrg) {
        RegisterVO registerVO = new RegisterVO();
        registerVO.setRegisterStatus(applicant.getRegisterStatus());
        if (applicant.getRegisterTitle() == TitleEnum.NO.getType()) {
            registerVO.setAwarded(false);
        } else {
            registerVO.setTitle(applicant.getRegisterTitle());
            registerVO.setTitleYear(applicant.getTitleYear());
            registerVO.setAwarded(true);
        }
        registerVO.setContactPerson(applicant.getContactPerson());
        registerVO.setContactPersonPhone(applicant.getContactPersonPhone());
        registerVO.setContactPerson(applicant.getContactPerson());
        registerVO.setContactPersonPhone(applicant.getContactPersonPhone());
        registerVO.setContactPersonPost(applicant.getContactPersonPost());
        registerVO.setContactPersonBirth(DateUtil.format(applicant.getContactPersonBirth(), DatePattern.NORM_DATE_FORMAT));
        registerVO.setPersonNumber(applicant.getPersonNumber());
        registerVO.setYouthNumber(applicant.getYouthNumber());
        registerVO.setPhone(applicant.getPhone());
        registerVO.setEmail(applicant.getEmail());
        registerVO.setAddress(applicant.getAddress());
        registerVO.setRemark(applicant.getRemark());
        registerVO.setOrgId(organization.getId());
        registerVO.setUsedName(applicant.getUsedName());
        registerVO.setOrgPath(Arrays.stream(StringUtils.split(organization.getOrgPath(), ","))
                .skip(1)
                .map(Long::valueOf)
                .toArray(Long[]::new));
        registerVO.setReviewType(applicant.getReviewType());
        registerVO.setReviewTypeEnabled(organization.getOrgType() == OrgTypeEnum.INDUSTRY.getCode());
        if (joinOrg != null) {
            registerVO.setJoinReviewOrgId(joinOrg.getId());
            registerVO.setJoinReviewOrgPath(Arrays.stream(StringUtils.split(joinOrg.getOrgPath(), ","))
                    .skip(2)
                    .map(Long::valueOf)
                    .toArray(Long[]::new));
        }
        return registerVO;
    }

}
